/**
 * Created by WangMing on 15/12/9.
 */



define([
    "../../service",
    "../../widgets/collectionWidget/collectionWidget",
    "../../widgets/sliderWidget/sliderWidget",
    "../../widgets/tab_widget/tab_widget"
], function(api) {

    // 定义所有相关的vmodel
    var vm = avalon.define({
        $id: "home",
        $notices: [],
        size: 0,
        $setting: {}

    });

    vm.$watch("setting", function(param) {
        vm.$setting = avalon.define(
            avalon.mix(vm.$setting, param))

    })


    return avalon.controller(function($ctrl) {
        // 视图渲染后，意思是avalon.scan完成
        $ctrl.$onRendered = function() {
            api.getItems({
                method: api.setPagePv,
            }, function(res) {

            })

        };
        // 进入视图
        $ctrl.$onEnter = function(param, rs, rj) {
            function MobileClient() {
                //以下是判断是否在移动客户端
                var sUserAgent = navigator.userAgent.toLowerCase();
                var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
                var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
                var bIsAndroid = sUserAgent.match(/android/i) == "android";
                if (bIsIpad || bIsIphoneOs || bIsAndroid) {
                    return true;
                }
                return false;
            }
            function getCookie(name)
            {
                var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
                if(arr=document.cookie.match(reg))
                    return unescape(arr[2]);
                else
                    return null;
            }
            // 首次进入页面跳转到校园看看
            console.log(document.cookie)
            if(MobileClient()){
                if((getCookie("avaJumpToWap"))!='Done'){
                    document.cookie = "avaJumpToWap=Done";
                    location.href=location.href.replace("#!","wap")
                }
            }
            return api.getItems({
                method: api.getNotices,

            }, function(res) {

                if (vm.$notices) vm.$notices = res.list||[];

                vm.size = (vm.$notices&&vm.$notices.length)||0
            })

        };
        // 对应的视图销毁前
        $ctrl.$onBeforeUnload = function() {

        };
        $ctrl.$vmodels = [vm];
    })
});
